package main

import (
	"sort"
)

type ListNode struct {
	Val  int
	Next *ListNode
}

func sortList(head *ListNode) *ListNode {
	result := make([]*ListNode, 0)

	for head != nil {
		result = append(result, head)
		head = head.Next
	}

	sort.Slice(result, func(i, j int) bool {
		if result[i].Val < result[j].Val {
			return true
		}

		return false
	})

	for i := 0; i < len(result)-1; i++ {
		result[i].Next = result[i+1]
	}

	if len(result) <= 0 {
		return nil
	}

	result[len(result)-1].Next = nil
	return result[0]
}

func main() {
	head := ListNode{Val: 4}
	node1 := ListNode{Val: 2}
	node2 := ListNode{Val: 1}
	node3 := ListNode{Val: 3}

	head.Next = &node1
	node1.Next = &node2
	node2.Next = &node3

	sortList(&head)

}
